#include "World.h"

Maze::Maze()
{
	thePlayer = new Player;
	m_bPlayerCreated = false;

	m_clppMyGrid = new Node*[WORLD_HEIGHT];
	for(int y = 0;y < WORLD_HEIGHT;++y)
	{
		m_clppMyGrid[y] = new Node[WORLD_WIDTH];
	}

	thePlayer->m_vPosition = D3DXVECTOR3((float)PosMod/2, 0.0f,(float)PosMod/2);
	for(int y = 0;y < WORLD_HEIGHT;++y)
	{
		for(int x = 0;x < WORLD_WIDTH;++x)
		{
			m_clppMyGrid[y][x].Init(x,y);
		}
	}
}

Maze::~Maze()
{
	for(int y = 0;y < WORLD_HEIGHT;++y)
	{
		delete [] m_clppMyGrid[y];
	}
	delete [] m_clppMyGrid;
}

void Maze::Reset()
{
	// As far as I can tell, the commented out code doesn't change anything - Strog

	/*m_clppMyGrid = new Node*[WORLD_HEIGHT];
	for(int y = 0;y < WORLD_HEIGHT;++y)
	{
		m_clppMyGrid[y] = new Node[WORLD_WIDTH];
	}*/

	thePlayer->m_vPosition = D3DXVECTOR3((float)PosMod/2, 0.0f,(float)PosMod/2);
	m_bPlayerCreated = false;
	for(int y = 0;y < WORLD_HEIGHT;++y)
	{
		for(int x = 0;x < WORLD_WIDTH;++x)
		{
			m_clppMyGrid[y][x].Init(x,y);
		}
	}

	Sleep(400);		// put in a sleep function here to avoid Input catching multiple key presses which keeps making new maps
}

void Maze::CreateMap(int x,int y,DrawList& drawlist)
{
	if(!m_bPlayerCreated)
	{
		drawlist.push_back(thePlayer);
		m_bPlayerCreated = true;
	}
	CheckDirections(x,y);

	while(m_clppMyGrid[y][x].CanGoDirection())
	{
		switch(rand() % 4)
		{
		case NORTH:
			if(m_clppMyGrid[y][x].CanGoDirection(NORTH))
			{
				m_clppMyGrid[y][x].BreakWall(NORTH);
				m_clppMyGrid[y-1][x].BreakWall(SOUTH);
				CreateMap(x,y-1,drawlist);
			}
			break;
		case SOUTH:
			if(m_clppMyGrid[y][x].CanGoDirection(SOUTH))
			{
				m_clppMyGrid[y][x].BreakWall(SOUTH);
				m_clppMyGrid[y+1][x].BreakWall(NORTH);
				CreateMap(x,y+1,drawlist);
			}
			break;
		case EAST:
			if(m_clppMyGrid[y][x].CanGoDirection(EAST))
			{
				m_clppMyGrid[y][x].BreakWall(EAST);
				m_clppMyGrid[y][x+1].BreakWall(WEST);
				CreateMap(x+1,y,drawlist);
			}
			break;
		case WEST:
			if(m_clppMyGrid[y][x].CanGoDirection(WEST))
			{
				m_clppMyGrid[y][x].BreakWall(WEST);
				m_clppMyGrid[y][x-1].BreakWall(EAST);
				CreateMap(x-1,y,drawlist);
			}
			break;
		}
		CheckDirections(x,y);
	}
}

//void Maze::SetListInfo(CharacterList Characters,StaticList Statics,ActivatorList Activators)
void Maze::SetListInfo(DrawList& drawlist)
{
	for(int y = 0;y < WORLD_HEIGHT;++y)	{
		for(int x = 0;x < WORLD_WIDTH;++x){
			for(int x2 = 0;x2 < AmountWalls;++x2){
				for(int y2 = 0;y2 < AmountWalls;++y2){
					if(m_clppMyGrid[y][x].CheckBrick(x2,y2))
						drawlist.push_back(m_clppMyGrid[y][x].GetBrick(x2,y2));
				}
			}
			//Still need my map to generate items
			/*for(int ItemAmount = 0;ItemAmount < MaxItems;++ItemAmount)
			{
				if(m_clppMyGrid[y][x].GetActivator(ItemAmount))
					drawlist.push_back(m_clppMyGrid[y][x].GetActivator(ItemAmount));
			}*/
		}
	}
}

D3DXVECTOR3 Maze::GetCenterofMap()
{
	m_vCenter.x = WORLD_WIDTH / 2 * PosMod + PosMod / 2;
	m_vCenter.y = 0;
	m_vCenter.z = WORLD_HEIGHT / 2 * PosMod + PosMod / 2;
	return m_vCenter;
}

void Maze::CheckDirections(int x,int y)
{
	//NORTH
	if((y - 1) >= 0 && !m_clppMyGrid[y - 1][x].wasVisited())
	{
		m_clppMyGrid[y][x].SetDirection(NORTH,true);
	}
	else
	{
		m_clppMyGrid[y][x].SetDirection(NORTH,false);
	}
	//SOUTH
	if((y + 1) < WORLD_HEIGHT && !m_clppMyGrid[y + 1][x].wasVisited())
	{
		m_clppMyGrid[y][x].SetDirection(SOUTH,true);
	}
	else
	{
		m_clppMyGrid[y][x].SetDirection(SOUTH,false);
	}
	//EAST
	if((x + 1) < WORLD_WIDTH && !m_clppMyGrid[y][x + 1].wasVisited())
	{
		m_clppMyGrid[y][x].SetDirection(EAST,true);
	}
	else
	{
		m_clppMyGrid[y][x].SetDirection(EAST,false);
	}
	//WEST
	if((x - 1) >= 0 && !m_clppMyGrid[y][x - 1].wasVisited())
	{
		m_clppMyGrid[y][x].SetDirection(WEST,true);
	}
	else
	{
		m_clppMyGrid[y][x].SetDirection(WEST,false);
	}
}